package com.lookme.lmtool.cracker.filter;

import com.lookme.lmtool.cracker.AbstractCracker;
import com.lookme.lmtool.cracker.CrackerConstants;
import com.lookme.lmtool.cracker.annotation.FilterAnnotation;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;

public abstract class AbstractFilter extends AbstractCracker implements IFilter{

    @Override
    public String getGroupName() {
        FilterAnnotation filterAnnotation=this.getClass().getAnnotation(FilterAnnotation.class);
        if(filterAnnotation!=null){
            return filterAnnotation.groupName();
        }
        return CrackerConstants.FILTER_GROUP_NAME_OTHER;
    }

    @Override
    public String getName() {
        FilterAnnotation filterAnnotation=this.getClass().getAnnotation(FilterAnnotation.class);
        if(filterAnnotation!=null){
            return filterAnnotation.name();
        }
        return null;
    }

    @XStreamAsAttribute
    boolean selected=true;

    transient BufferedImage image;

    @Override
    public boolean isSelected() {
        return selected;
    }

    @Override
    public void setSelected(Boolean newValue) {
        this.selected=newValue;
    }

    @Override
    public BufferedImage getImage() {
        return image;
    }

    @Override
    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public BufferedImage filter( BufferedImage src, BufferedImage dst ){
       return getFilter().filter(src,dst);
    }


    @Override
    public BufferedImageOp getFilter() {
        FilterAnnotation filterAnnotation=this.getClass().getAnnotation(FilterAnnotation.class);
        if(filterAnnotation!=null){
            try {
                return (BufferedImageOp) filterAnnotation.filterClass().newInstance();
            } catch (Exception e) {
            }
        }
        return null;
    }
}
